﻿using BigEgg.Framework.UnitTesting;
using FMStudio.Documents.TemplateDocument.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Linq;

namespace FMStudio.Documents.TemplateDocument.Test
{
    [TestClass]
    public class ProjectTemplateDocumentTest
    {
        private string TestPath1 = Path.Combine(Environment.CurrentDirectory, "Template1.fmstemplate");
        private string TestPath2 = Path.Combine(Environment.CurrentDirectory, "Template2.fmstemplate");


        [TestInitialize]
        public void TestCleanup()
        {
            if (File.Exists(TestPath1))
                File.Delete(TestPath1);
            if (File.Exists(TestPath2))
                File.Delete(TestPath2);
        }


        [TestMethod]
        public void DocumentTest()
        {
            ProjectTemplateDocument document = new ProjectTemplateDocument();

            Assert.AreEqual(TemplateType.Project, document.Type);
            Assert.IsNotNull(document.TemplateContent);
        }

        [TestMethod]
        public void DocumentModifyTest()
        {
            TemplateDocumentType documentType = new TemplateDocumentType();
            ProjectTemplateDocument document = documentType.New(TestPath1) as ProjectTemplateDocument;
            documentType.Save(document);

            Assert.IsFalse(document.IsModified);
            AssertHelper.PropertyChangedEvent(document, x => x.IsModified, () => document.TemplateContent.TargetName = "NewName.fmproj");
            Assert.IsTrue(document.IsModified);

            documentType.Save(document);

            Assert.IsFalse(document.IsModified);
            AssertHelper.PropertyChangedEvent(document, x => x.IsModified, 
                () => document.TemplateContent.Items.Add(new Folder() { Name = "Folder", TargetFolderName = "NewFolder" }));
            Assert.IsTrue(document.IsModified);
        }


        [TestClass]
        public class ProjectTest
        {
            [TestMethod]
            public void PropertiesWithNotification()
            {
                ProjectTemplateDocument.Project project = new ProjectTemplateDocument.Project();

                AssertHelper.PropertyChangedEvent(project, x => x.Name, () => project.Name = "project");
                Assert.AreEqual("project", project.Name);

                AssertHelper.PropertyChangedEvent(project, x => x.TargetName, () => project.TargetName = "TargetProject");
                Assert.AreEqual("TargetProject", project.TargetName);

                AssertHelper.PropertyChangedEvent(project, x => x.Items,
                    () => project.Items.Add(new Folder() { Name = "SomeFolder" }));
                Assert.AreEqual(1, project.Items.Count);
            }

            [TestMethod]
            public void ItemsTest()
            {
                ProjectTemplateDocument.Project project = new ProjectTemplateDocument.Project();
                Assert.IsNotNull(project.Items);

                project.Items.Add(new Folder() { Name = "SomeFolder" });
                project.Items.Add(new ProjectItem() { Name = "SomeFile" });

                Assert.AreEqual(2, project.Items.Count);

                project.Items.Remove(project.Items.First(x => x is Folder));
                Assert.AreEqual(1, project.Items.Count);
            }
        }
    }
}
